using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ContentListSummary : AdminBaseUserControl
    {
        public event EventHandler EditContent;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindContent(object sender, EventArgs e)
        {
            ClassList ucClassList = (ClassList)sender;
            BindContent(Convert.ToInt64(ucClassList.SelectedValue));
        }

        public void BindContent(long classID)
        {
            try
            {

                ICmsClass clazz = ClassController.GetClass(classID);
                lblStatus.Text = String.Format("Content List of Class {0}",clazz.Name);
                Session[SessionVariable.Clazz.CurrentID] = classID;
                gvContent.DataSource = ContentController.GetContentList(classID);
                gvContent.DataBind();
                ViewState["ContentID"] = null;
            }
            catch(Exception ex)
            {
                Logger.Error("[[Error]]", ex);
            }
        }

        public void DoDelete(object sender, EventArgs e)        
        {
            try
            {

                if (ViewState["ContentID"] != null) ContentController.DeleteContent(Convert.ToInt64(ViewState["ContentID"]));
                BindContent(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            }
            catch (Exception ex)
            {
                Logger.Error("[[Error]]", ex);
            }
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            BindContent(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));

        }

        public string SelectedID
        {
            get { return Convert.ToString(ViewState["ContentID"]); }
            set { ViewState["ContentID"] = value; }
        }

        protected void gvContent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                long ID = Convert.ToInt64(e.CommandArgument);
                ViewState["ContentID"] = ID;
                if (e.CommandName == "editContent")
                {
                    if (EditContent != null)
                    {
                        EditContent(this, new EventArgs());
                    }
                }
                if (e.CommandName == "delContent")
                {
                    PageController.DeletePage(Convert.ToInt64(ID));
                    BindContent(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                }
            }
            catch (Exception ex)
            {
                Logger.Error("[[Error]]", ex);
            }
        }

        
    }
}